package com.ems.controller;

import com.ems.config.LogPointer;
import com.ems.entity.MerchantEntity;
import com.ems.entity.UserEntity;
import com.ems.service.IMerchantService;
import com.ems.util.RedisUtils;
import com.ems.vo.BaseResult;
import com.ems.vo.MerchantVo;
import com.ems.vo.UserVo;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商户/部门表 前端控制器
 * </p>
 *
 * @author WuZH
 * @since 2023-10-24
 */
@RestController
@RequestMapping ( "/merchant" )
@CrossOrigin ( value = "*", maxAge = 3600 )
public class MerchantController {

    @Resource
    private IMerchantService iMerchantService;

    @RequestMapping ( "queryList" )
    @LogPointer ( "商户列表" )
    public BaseResult queryList ( @RequestBody MerchantVo merchantVo ) {
        List<MerchantVo> merchantVos = iMerchantService.queryList ( merchantVo );
        return BaseResult.success ( merchantVos, iMerchantService.queryCount ( merchantVo ) );
    }

    @RequestMapping ( "saveSingle" )
    @LogPointer ( "新增商户" )
    public BaseResult saveSingle ( @RequestBody MerchantVo merchantVo ) {
        merchantVo = iMerchantService.saveSingle ( merchantVo );
        return BaseResult.success ( merchantVo );
    }


    @RequestMapping ( "queryMerchant" )
    @LogPointer ( "查询当前登录商户" )
    public BaseResult queryMerchant ( @RequestBody MerchantVo merchantVo ) {
        // 获取当前登录用户信息
        UserVo userVo = ( UserVo ) RedisUtils.get ( merchantVo.getToken () );
        merchantVo.setId ( userVo.getMerchantId () );
        merchantVo = iMerchantService.querySingle ( merchantVo );
        return BaseResult.success ( merchantVo );
    }

}
